package fyp.spedmew;

import android.app.ListFragment;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;

import fyp.spedmew.backend.entity.jobEndpoint.model.Job;
import fyp.spedmew.backend.entity.userEndpoint.model.User;
import fyp.spedmew.connector.JobListATask;
import fyp.spedmew.connector.UserFindByIdATask;

/**
 * Created by chengys on 17/12/2014.
 */
public class JobReceivedListDispatcherListFragment extends ListFragment {

    private List<ListViewClass> mItems;        // ListView items list

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // initialize the items list
        mItems = new ArrayList<ListViewClass>();
        Resources resources = getResources();

        try {
            List<Job> list;
            list = new JobListATask().execute().get();

            {
                for (Job job : list) {
                    User dispatcher = new UserFindByIdATask().execute(job.getRecipientId()).get();
                    ListViewClass item = new ListViewClass();
                    item.setJobObject(job);
                    item.setTitle(job.getTitle());
                    item.setDescription(job.getDetail());
                    item.setName(dispatcher.getName());
                    item.setPrice("$ " + job.getPrice());
                    item.setIcon(resources.getDrawable(R.drawable.coffee));
                    item.setStatus_icon(resources.getDrawable(R.drawable.aol));
                    item.setAlertIcon(resources.getDrawable(R.drawable.blank));
                    item.setPic(job.getTitle());
                    item.setStatus("Status");
                    mItems.add(item);
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (NullPointerException n){
            ListViewClass item = new ListViewClass();
        }
        // initialize and set the list adapter
        setListAdapter(new ListViewArrayAdapter(getActivity(), mItems));
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        // remove the dividers from the ListView of the ListFragment
        getListView().setDivider(null);
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        // retrieve theListView item
        ListViewClass item = mItems.get(position);
    }
}

